<!-- 名师专项课详情 -->
<template>
  <div class="app-container">
    <div class="stage">
      <div class="head_cover">
        <img v-show="show" :src="List.cover" />
        <div v-show="!show" id="player"></div>
      </div>
      <div class="p15 clearfix">
        <div class="class_head_title textoverflow2">{{ List.name }}</div>
        <div class="class_head_left">
          <div class="class_head_number">
            <i style="font-size: 12px; font-weight: 400">抢购价：¥</i>
            <i>{{ List.price }}</i>
          </div>
          <div class="class_head_men">
            <van-icon :name="require('@/assets/images/xuexirenshu.png')" />
            <span>{{ List.interact.stud_num }}人已学习</span>
          </div>
        </div>
      </div>
    </div>

    <van-tabs :swipe-threshold="1" v-model="active" sticky swipeable>
      <van-tab title="课程详情">
        <div v-html="List.content" class="mt10"></div>
      </van-tab>
      <van-tab title="课程目录" class="mulu">
        <van-collapse v-model="activeNames">
          <van-collapse-item
            v-for="(item, index) in List.tree"
            :key="index"
            :title="item.name"
            :icon="require('@/assets/images/shiping.png')"
            :name="index + 1"
          >
            <div
              v-for="(les, index) in item.lessons"
              :key="index"
              class="collapse_div"
              @click="watch(les)"
            >
              <div class="collapse_name">{{ les.name }}</div>
              <div style="margin-top: 5px">
                <span class="collapse_time">
                  <van-icon name="underway-o" />
                  <span>{{ les.vod_duration }}</span>
                </span>
                <!-- <van-button round class="collapse_button">课后习题</van-button>
                <van-button round class="collapse_button">预习课件</van-button>-->
              </div>
            </div>
          </van-collapse-item>
        </van-collapse>
      </van-tab>
      <van-tab title="授课讲师" style="padding: 10px">
        <!-- <package-picker /> -->
        <van-swipe :loop="false" :show-indicators="false">
          <van-swipe-item
            v-for="(item, index) in List.teachers"
            :key="index"
            @click="toTeachersInfo(item)"
          >
            <div class="pack_live_left">
              <img :src="item.avatar" class="pack_live_touxiang" />
              <div style="padding-left: 12px">
                <div class="pack_live_name">{{ item.name }}</div>
                <div class="pack_live_name1">{{ item.cert }}</div>
              </div>
            </div>
            <div class="pack_live_content">{{ item.intro }}</div>
          </van-swipe-item>
        </van-swipe>
      </van-tab>
    </van-tabs>

    <div class="footer">
      <div class="class_head_right" @click="favClick(interactList.is_fav)">
        <img
          :src="
            interactList.is_fav == 1
              ? require('@/assets/images/shoucang1.png')
              : require('@/assets/images/shoucang.png')
          "
          alt
        />
        <div class="shouc">{{ interactList.is_fav == 1 ? '已收藏' : '收藏' }}</div>
      </div>

      <van-button
        v-if="List.price == '0.00' || List.is_buy == 1"
        type="primary"
        round
        @click="stydyClick"
      >立即学习</van-button>
      <van-button v-else type="primary" round @click="handlezhifu">立即购买</van-button>
    </div>
  </div>
</template>

<script>
import { Toast } from 'vant'
import moment from 'moment'
import { parseFav } from '@/utils/status/user'
// import PackagePicker from '@/components/Picker/packagePicker'
// import { viewFreevod, favMine, getVideoToken, commonShorturl } from '@/api/home/product-list'
import { viewFreevod, favMine, getVideoToken } from '@/api/goods/open'
export default {
  name: 'OpenDetails',
  components: {
    // PackagePicker
  },
  data() {
    return {
      queryParams: {
        goods_id: null
      },
      List: {
        interact: {}
      },
      treeList: [],
      teachersList: [],
      interactList: [],
      active: 0,
      activeNames: [1],
      // 保利威
      show: true,
      vodPlayerJs: 'https://player.polyv.net/script/player.js',
      playerJs: 'https://player.polyv.net/resp/live-h5-player/latest/liveplayer.min.js',
      playerList: {
        vid: '',
        playsafe: '',
        ts: '',
        sign: '',
        uid: '',
        channel_id: ''
      },
      showShare: false,
      showFenXiang: false,
      // options: [
      //   { name: '推荐奖励', icon: require('../../../../assets/img/tuijianjiangli.png') },
      //   { name: '复制链接', icon: require('../../../../assets/img/fuzhilianjie.png') },
      //   { name: '分享好友', icon: require('../../../../assets/img/fenxianghaoyou.png') },
      //   { name: '生成海报', icon: require('../../../../assets/img/shengchenghaibao.png') }
      // ],
      windowUrl: window.location.href,
      shareUrl: '',
      // 海报
      showOverlay: false,
      canvasShow: true,
      imgUrl: '',
      // 海报文字
      name1: null,
      name2: null
    }
  },

  computed: {
    loginList() {
      return this.$store.getters.loginList
    },
    type() {
      return this.$store.getters.type
    },
    profileList() {
      return this.$store.getters.profileList
    },
    share_token() {
      return this.$store.getters.share_token
    }
  },

  mounted() {
    this.queryParams.goods_id = this.$route.query.id
    this.fetchList()
  },

  methods: {
    // 初始化数据
    fetchList() {
      viewFreevod(this.queryParams).then(res => {
        this.List = res.data
        this.treeList = res.data.tree
        this.teachersList = res.data.teachers
        this.interactList = res.data.interact
        this.treeList.map(b => {
          b.beg_time =
            b.beg_time == 0
              ? moment.utc(b.vod_duration * 1000).format('HH:mm:ss')
              : moment(b.beg_time * 1000).format('YYYY-MM-DD HH:mm')
        })
        if (this.List.name.length > 20) {
          console.log(this.List.name, 123465)
          this.name1 = this.List.name.substr(0, 20)
          this.name2 = this.List.name.substr(20, this.List.name.length)
          console.log(this.name1, this.name2)
        } else {
          this.name1 = this.List.name
        }
        // this.$getWxApplets(window.location.href, this.List.name, this.List.cover)
      })
    },
    // 预习课件
    toPreviewUrl(url) {
      const a = document.createElement('a')
      a.href = url
      a.click()
    },
    // toWxOpen() {
    //   debugger
    //   const btn = document.getElementById('launch-btn')
    //   btn.addEventListener('launch', function (e) {
    //     alert('success', e)
    //   })
    //   btn.addEventListener('error', function (e) {
    //     alert('fail', e.detail)
    //   })
    // },
    // 确认支付
    handlezhifu() {
      this.$set(this.List, 'sku_id', 0)
      this.$set(this.List, 'path', '/open-detail')
      this.$set(this.List, 'query', this.$route.query)
      this.$set(this.List, 'share_token', this.$route.query.token || '')
      this.$store.dispatch('home/setGoodsList', this.List)
      this.$router.push({ path: '/confirm-order' })
    },
    // 收藏
    favClick(row) {
      const { action, result, actionTip } = parseFav(row)
      const data = {
        goods_id: this.queryParams.goods_id,
        action: action
      }
      favMine(data).then(res => {
        this.interactList.is_fav = result
        Toast({
          message: actionTip
        })
      })
    },
    // 名师团队
    toTeachersInfo(row) {
      this.$router.push({ name: 'TeachersInfo', query: row })
    },

    // 立即学习
    stydyClick() {
      if (this.treeList.length === 0) {
        return this.$toast('暂无视频')
      }
      this.active = 1
      this.watch(this.treeList[0])
    },
    parseStatus(item) {
      switch (item.status) {
        case 0:
          return item.beg_time
        case 1:
          return '直播中'
        case 2:
          return moment.utc(item.vod_duration * 1000).format('mm分ss秒')
      }
    },

    // 观看视频
    watch(row) {
      if (!this.show) {
        this.player.destroy()
      }
      if (row.status === 2) {
        const data = {
          vid: row.vid,
          goods_id: this.queryParams.goods_id,
          lesson_id: row.id
        }
        getVideoToken(data).then(res => {
          console.log(res, '1')
          this.show = false
          this.playerList.vid = res.data.videoId
          this.playerList.playsafe = res.data.token
          this.playerList.ts = res.data.ts
          this.playerList.sign = res.data.sign
          this.playerList.uid = res.data.userId
          this.loadPlayerScript(this.loadPlayer)
        })
      } else {
        const data = {
          vid: null,
          goods_id: this.queryParams.goods_id,
          lesson_id: row.id
        }
        getVideoToken(data).then(res => {
          this.show = false
          this.playerList.channel_id = row.channel_id
          this.loadPlayerScript1(this.loadPlayer1)
        })
      }
    },
    // 点播
    loadPlayerScript(callback) {
      if (!window.polyvPlayer) {
        const myScript = document.createElement('script')
        myScript.setAttribute('src', this.vodPlayerJs)
        myScript.onload = callback
        document.body.appendChild(myScript)
      } else {
        callback()
      }
    },
    loadPlayer() {
      const polyvPlayer = window.polyvPlayer
      this.player = polyvPlayer({
        wrap: '#player',
        width: '100%',
        height: '100%',
        autoplay: true,
        vid: this.playerList.vid,
        playsafe: this.playerList.playsafe,
        ts: this.playerList.ts,
        sign: this.playerList.sign,
        uid: this.playerList.uid
      })
    },
    // 直播
    loadPlayerScript1(callback) {
      if (!window.polyvLivePlayer) {
        const myScript = document.createElement('script')
        myScript.setAttribute('src', this.playerJs)
        myScript.onload = callback
        document.body.appendChild(myScript)
      } else {
        callback()
      }
    },
    loadPlayer1() {
      const polyvLivePlayer = window.polyvLivePlayer
      this.player = polyvLivePlayer({
        wrap: '#player',
        width: '100%',
        height: '100%',
        autoplay: true,
        vid: this.playerList.channel_id,
        uid: '0a4b11bac9'
      })
    }
    // 老详情
    // toLecturerDetails(row) {
    //   this.$router.push({ path: '/lecturer-details', query: row })
    // }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  padding-bottom: 85px;
}
.head_cover {
  padding-bottom: 56%;
  width: 100%;
  position: relative;
  img {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
  }
  #player {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
  }
}

.class_head_title {
  margin-top: 15px;
  font-size: 16px;
  font-weight: bold;
  color: #333333;
  line-height: 24px;
}
.class_head_left {
  display: flex;
  justify-content: space-between;
  margin-top: 5px;
  padding-bottom: 10px;
  line-height: 30px;
  .class_head_number {
    display: inline-block;
    font-size: 20px;
    font-weight: 800;
    color: #11a8a4;
    // line-height: 30px;
  }
  .class_head_men {
    margin-left: 15px;
    font-size: 12px;
    color: #999999;
    .van-icon {
      font-size: 15px;
      margin-right: 4px;
      vertical-align: text-bottom;
    }
  }
}
::v-deep .stage {
  background: #ffffff;

  .van-divider::after,
  .van-divider::before {
    border-width: 8px 0 0;
    border-color: #f6f6f6;
  }
}

.class_head_title {
  margin-top: 18px;
  font-size: 16px;
  font-weight: bold;
  color: #333333;
  line-height: 26px;
}
.yuyue {
  float: left;
  font-size: 12px;
  color: #999999;
  .van-icon {
    vertical-align: middle;
    margin-bottom: 2px;
    // margin-right: 2px;
  }
  .class_head_number {
    display: inline-block;
    font-size: 20px;
    font-weight: 800;
    color: #11a8a4;
    margin-right: 10px;
    // line-height: 30px;
  }
  .van-button {
    width: 1.46667rem;
    height: 0.74667rem;
    background: #fd953f;
    padding: 0;
    vertical-align: bottom;
    font-size: 0.37333rem;
    font-weight: bold;
    color: #fffefe;
    margin-right: 10px;
  }
}
.class_head_right {
  float: right;
  text-align: center;
  img {
    width: 17px;
    height: 17px;
  }
  .shouc {
    font-size: 12px;
    color: #666666;
  }
}

::v-deep .van-tabs {
  margin-top: 5px;
  padding-bottom: 90px;
  background: #ffffff;

  .van-tabs__wrap {
    border-bottom: 1px solid #ddd;
  }
  .van-tabs__line {
    background-color: #11a8a4;
  }
  .van-tabs__content {
    min-height: 200px;
  }
}

.collapse_div {
  border-bottom: 0.1px solid rgba(246, 246, 246, 1);
  padding-top: 10px;
  padding-bottom: 10px;
  .collapse_name {
    font-size: 13px;
    color: #333333;
    line-height: 19px;
  }
  .van-icon {
    font-size: 12px;
    margin-top: 2px;
    margin-right: 5px;
  }
}

.collapse_time {
  font-size: 12px;
  font-weight: 400;
  color: #999999;
}

// 金牌讲师
::v-deep .pack_live {
  // margin-bottom: 50px;
  .van-swipe__track {
    width: 100% !important;
    margin-top: 8px;
  }
  .pack_live_div {
    // display: flex;
    box-sizing: border-box;
    width: 100% !important;
    height: 201px;
    padding: 25px 21px 0 22px;
    // background: url('../../../../assets/img/home-jiangshi21.png');
    background-size: 100% 100%;
    border-radius: 10px;
    .pack_live_left {
      display: flex;
      .pack_live_touxiang {
        width: 66px;
        height: 66px;
        border-radius: 50%;
      }
      .pack_live_name {
        font-size: 14px;
        font-weight: bold;
        color: #333333;
      }
      .pack_live_name1 {
        font-size: 13px;
        color: #333333;
      }
    }
    .pack_live_content {
      margin-top: 16px;
      font-size: 14px;
      color: #333333;
      line-height: 22px;
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-line-clamp: 3;
      -webkit-box-orient: vertical;
    }
  }
}

// 目录
.mulu {
  margin-top: 10px;
  .mulu_title {
    margin-bottom: 8px;
    font-size: 14px;
    font-weight: 500;
    color: #333333;
  }
  .collapse_time {
    font-size: 12px;
    font-weight: 400;
    color: #999999;
    line-height: 20px;
    .van-icon {
      vertical-align: middle;
      margin-bottom: 2px;
      margin-right: 5px;
    }
  }
  .collapse_button {
    margin-left: 5px;
    float: right;
    // width: 65px;
    height: 21px;
    background: #11a8a4;
    border-radius: 11px;
    font-size: 12px;
    font-weight: 500;
    color: #ffffff;
  }
}

.footer {
  box-sizing: border-box;
  position: fixed;
  bottom: 0;
  display: flex;
  justify-content: space-between;
  width: 100%;
  height: 80px;
  padding: 15px;
  background: #ffffff;
  box-shadow: 0px -1px 4px 0px rgba(66, 66, 66, 0.19);
  .class_head_right {
    padding-top: 2px;
    width: 45px;
    text-align: center;
    img {
      width: 20px;
      height: 20px;
    }
    .shouc {
      font-size: 14px;
      font-weight: 400;
      color: #666666;
      line-height: 14px;
    }
  }
  .van-button {
    width: 208px;
    height: 46px;
    background: #009697;
    border-color: #009697;
    font-size: 16px;
    font-weight: bold;
  }
}

::v-deep .bottom .van-row {
  height: 100%;
  line-height: 70px;
  text-align: center;
  .van-button {
    width: 225px;
    height: 44px;
    background: #009697;
    border-radius: 22px;
    font-size: 16px;
    font-weight: bold;
  }
}

::v-deep .van-popup {
  // height: 300px;
  // text-align: center;
  // .title {
  //   padding: 0 15px;
  //   text-align: left;
  //   font-size: 14px;
  //   font-weight: 500;
  //   color: #666666;
  //   line-height: 21px;
  // }
  // .van-grid {
  //   margin-top: 10px;
  //   .van-grid-item {
  //     cursor: pointer;
  //   }
  //   img {
  //     width: 44px;
  //     height: 44px;
  //   }
  //   .van-grid-item__text {
  //     font-size: 14px;
  //     font-weight: 500;
  //     color: #666666;
  //   }
  //   .van-grid-item__content::after {
  //     border: none;
  //   }
  // }
  // .xuxian {
  //   margin: 10px 0;
  //   border-bottom: 1px dashed #eeeeee;
  // }
  // .van-button {
  //   width: 345px;
  //   height: 44px;
  //   background: #009697;
  //   font-size: 16px;
  //   font-weight: 500;
  //   color: #fffefe;
  // }

  .van-share-sheet__title {
    font-size: 18px;
    font-weight: bold;
    color: #333333;
  }
  .van-share-sheet__description {
    font-size: 14px;
    font-weight: 500;
    color: #666666;
  }
  .van-share-sheet__options {
    justify-content: space-around;
  }
}

::v-deep .sharediv {
  .van-popup {
    height: 300px;
    text-align: center;
    .title {
      padding: 0 15px;
      text-align: left;
      font-size: 14px;
      // font-weight: 500;
      color: #666666;
      line-height: 21px;
    }
    .van-grid {
      margin-top: 10px;
      .van-grid-item {
        cursor: pointer;
      }
      img {
        width: 44px;
        height: 44px;
      }
      .van-grid-item__text {
        font-size: 14px;
        // font-weight: 500;
        color: #666666;
      }
      .van-grid-item__content::after {
        border: none;
      }
    }
    .xuxian {
      margin: 10px 0;
      border-bottom: 1px dashed #eeeeee;
    }
    .van-button {
      width: 345px;
      height: 44px;
      background: #009697;
      font-size: 16px;
      font-weight: 500;
      color: #fffefe;
    }
  }
}

.action_button {
  width: 165px;
  height: 33px;
  margin-bottom: 15px;
  line-height: 33px;
  background: #f2f2f5;
  // border-radius: 17px;
}
.skuActive {
  color: #ffffff;
  background: #11a8a4;
}
.action_zhifu {
  display: block;
  width: 270px;
  height: 44px;
  margin: 0 auto 12px;
  background: #009697;
  border-radius: 22px;
  font-size: 16px;
  font-weight: 500;
  color: #fffefe;
}

::v-deep .wrapper {
  // display: flex;
  // align-items: center;
  // justify-content: center;
  height: 100%;
  h2 {
    margin-top: 6%;
    font-size: 20px;
    font-weight: 800;
    text-align: center;
    color: #ffffff;
    line-height: 18px;
  }
  .van-icon {
    display: table;
    margin: 10px auto;
  }
}

.block1_div {
  height: 80%;
  display: flex;
  align-items: center;
  justify-content: center;
  img {
    z-index: 99999;
  }
}
.block1 {
  display: block;
  width: 290px;
  height: 500px;
  border-radius: 10px;
  margin: auto;
}

.loading {
  height: 100%;
  text-align: center;
  padding-top: 40%;
}

.block {
  position: fixed;
  left: 9999px;
  width: 360px;
  height: 685px;
  margin: auto;
  text-align: center;
  border-radius: 10px;
  // background: url('../../../../assets/img/haibao.png');
  background-size: 100% 100%;
  .touxiang {
    display: inline-block;
    margin-top: 26px;
    width: 80px;
    height: 80px;
    border-radius: 50%;
  }
  .name {
    margin-top: 5px;
    font-size: 18px;
    font-weight: bold;
    color: #ffffff;
    line-height: 28px;
  }
  .name1 {
    font-size: 16px;
    font-weight: 500;
    color: #ffffff;
    line-height: 16px;
  }
  .sanjiao {
    width: 19px;
    height: 16px;
    margin: 10px auto 0;
    // background: url('../../../../assets/img/haibao-3.png');
    background-size: 100% 100%;
  }
  .course {
    box-sizing: border-box;
    height: 440px;
    // padding: 10px;
    background: #ffffff;
    box-shadow: 0px 0px 13px 0px rgba(81, 17, 0, 0.13);
    border-radius: 10px;
    .img1 {
      margin-top: 10px;
      width: calc(100% - 20px);
      height: 200px;
      border-radius: 10px;
      overflow: hidden;
    }
    .title {
      padding: 0px 0px 6px 10px;
      text-align: left;
      font-size: 16px;
      font-weight: bold;
      color: #333333;
      line-height: 20px;
    }
    .jiage {
      padding-left: 10px;
      text-align: left;
      font-size: 20px;
      font-weight: 800;
      color: #ff0000;
      line-height: 28px;
    }
    .img2 {
      display: inline-block;
      margin: 6px auto 0;
      width: 80px;
      height: 80px;
    }
    .van-button {
      display: block;
      margin: 7px auto;
      height: 27px;
      font-size: 14px;
      font-weight: bold;
      color: #f77b0d;
      line-height: 13px;
      background: linear-gradient(7deg, #ffe5de, #fff0dd);
      .van-button__text {
        padding-bottom: 8px !important;
      }
    }
  }
  .logo-hengfu {
    width: 150px;
    height: 22px;
    margin: 17px auto;
    // background: url('../../../../assets/img/logo-hengfu.png');
    background-size: 100% 100%;
  }
}

.fenxiang-bgm {
  height: 40%;
  // background: url('../../../../assets/img/fenxiang-bgm.png');
  background-size: 100% 100%;
}

.z9999 {
  z-index: 9999 !important;
}

.van-swipe-item {
  // display: flex;
  box-sizing: border-box;
  // width: 290px !important;
  height: 182px;
  padding: 15px;
  background: url('../../../assets/images/home_teacher_box.png');
  background-size: 100% 100%;
  border-radius: 5px;
  box-shadow: 0 3px 9px 0 rgb(50 50 50 / 8%);
  .pack_live_left {
    display: flex;
    .pack_live_touxiang {
      width: 66px;
      height: 66px;
      border-radius: 50%;
    }
    .pack_live_name {
      font-size: 14px;
      font-weight: 600;
      color: #333333;
      line-height: 38px;
    }
    .pack_live_name1 {
      font-size: 14px;
      font-weight: 400;
      color: #333333;
      line-height: 18px;
    }
  }
  .pack_live_content {
    margin-top: 12px;
    font-size: 13px;
    font-weight: 400;
    color: #666666;
    line-height: 22px;
    @include textoverflow(3);
  }
}
</style>
